package gui 
{
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class PositionInfo
	{
		private var _posX:Number;
		private var _posY:Number;
		/*private var _isRelativeX:Boolean;
		private var _isRelativeY:Boolean;*/
		private var _centeredByX:Boolean;
		private var _centeredByY:Boolean;
		private var _align:int = 1;
		public static const ALIGN_NONE:int = 0;
		public static const ALIGN_CENTR_X:int = 1;
		public static const ALIGN_CENTR_Y:int = 2;
		public static const ALIGN_TOP:int = 4;
		public static const ALIGN_BOTTOM:int = 8;
		public static const ALIGN_LEFT:int = 16;
		public static const ALIGN_RIGHT:int = 32;
		
		private var _leftPadding:Number = 0;
		private var _rightPadding:Number = 0;
		private var _topPadding:Number = 0;
		private var _bottomPadding:Number = 0;
		public var showLogInfo:Boolean = false;
		
		public function PositionInfo(x:Number = 0, y:Number = 0, alignType:int = ALIGN_NONE) 
		{
			_posX = x;
			_posY = y;
			_align = alignType;
			centeredByX = true;
			centeredByY = true;
		}
		
		private function showLog(str:String):void
		{
			if (showLogInfo)
			{
				trace(this + ": " + str);
			}
		}
		
		/**
		 * Example: left to right
		 */
		public function reverseX():void {
			if ((_align & ALIGN_LEFT) > 0) {
				_align = _align & ~ALIGN_LEFT;
				addAlign(ALIGN_RIGHT);
				_rightPadding = _leftPadding;
			}else if ((_align & ALIGN_RIGHT) > 0) {
				_align = _align & ~ALIGN_RIGHT;
				addAlign(ALIGN_LEFT);
				_leftPadding = _rightPadding;
			}else if(!((_align & ALIGN_CENTR_X) > 0)){
				addAlign(ALIGN_RIGHT);
				_rightPadding = posX;
			}
			
		}
		
		public function getPosX(parentWidth:Number, objectWidth:Number):Number
		{
			var xCalc:Number = _posX;
			showLog("align: " + align + " left padding: " + leftPadding);
			if ((_align & ALIGN_LEFT) > 0)
			{
				showLog("set left padding: " + align);
				xCalc = _leftPadding;
			}else if((_align & ALIGN_RIGHT) > 0)
			{
				xCalc = parentWidth - objectWidth - _rightPadding;
			}else if ((_align & ALIGN_CENTR_X) > 0)
			{
				showLog("parentW: " + parentWidth + " objW: " + objectWidth);
				if (centeredByX)
				{
					xCalc = (parentWidth - objectWidth) * 0.5;
				}else
				{
					xCalc = parentWidth  * 0.5;
				}
				showLog("parentW: " + parentWidth + " objW: " + objectWidth + " calX: " + xCalc);
			}
			
			return xCalc;
		}
		
		public function getPosY(parentHeight:Number, objectHeight:Number):Number
		{
			var yCalc:Number = _posY;
			if ((_align & ALIGN_TOP) > 0)
			{
				yCalc = _topPadding;
			}else if((_align & ALIGN_BOTTOM) > 0)
			{
				yCalc = parentHeight - objectHeight - _bottomPadding;
				showLog("ycalc: " + yCalc + " parent h:" + parentHeight);
			}else if ((_align & ALIGN_CENTR_Y) > 0)
			{
				if (centeredByY)
				{
					yCalc = (parentHeight - objectHeight) * 0.5;
				}else
				{
					yCalc = parentHeight * 0.5;
				}
			}
			return yCalc;
		}
		
		public function get posX():Number 
		{
			return _posX;
		}
		
		public function set posX(value:Number):void 
		{
			_posX = value;
		}
		
		public function get posY():Number 
		{
			return _posY;
		}
		
		public function set posY(value:Number):void 
		{
			_posY = value;
		}
		
		
		public function get leftPadding():Number 
		{
			return _leftPadding;
		}
		
		public function set leftPadding(value:Number):void 
		{
			_leftPadding = value;
		}
		
		public function get rightPadding():Number 
		{
			return _rightPadding;
		}
		
		public function set rightPadding(value:Number):void 
		{
			_rightPadding = value;
		}
		
		public function get topPadding():Number 
		{
			return _topPadding;
		}
		
		public function set topPadding(value:Number):void 
		{
			_topPadding = value;
		}
		
		public function get bottomPadding():Number 
		{
			return _bottomPadding;
		}
		
		public function set bottomPadding(value:Number):void 
		{
			_bottomPadding = value;
		}
		
		public function get align():int 
		{
			return _align;
		}
		
		public function set align(value:int):void 
		{
			
			_align = value;
		}
		
		public function get centeredByX():Boolean 
		{
			return _centeredByX;
		}
		
		public function set centeredByX(value:Boolean):void 
		{
			_centeredByX = value;
		}
		
		public function get centeredByY():Boolean 
		{
			return _centeredByY;
		}
		
		public function set centeredByY(value:Boolean):void 
		{
			_centeredByY = value;
		}
		
		public function addAlign(alignType:int):void
		{
			_align = _align | alignType;
		}
		
		public function clone():PositionInfo
		{
			var posNew:PositionInfo = new PositionInfo();
			posNew.posX = this.posX;
			posNew.posY = this.posY;
			posNew.align = this.align;
			posNew.leftPadding = this.leftPadding;
			posNew.rightPadding = this.rightPadding;
			posNew.topPadding = this.topPadding;
			posNew.bottomPadding = this.bottomPadding;
			posNew.centeredByX = centeredByX;
			posNew.centeredByY = centeredByY;
			
			return posNew;
		}
		
	}

}